#!/bin/bash
#
#    Copyright 2009 Spencer B. Koehler, Ph.D.
#
#    This file is part of the Brillig Toolkit.
#
#    The Brillig Toolkit is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Lesser General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    The Brillig Toolkit is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Lesser General Public License for more details.
#
#    You should have received a copy of the GNU Lesser General Public License
#    along with The Brillig Toolkit.  If not, see <http://www.gnu.org/licenses/>.
#
# add-license
#
# Add the license text to the source-code file argument.
# The license to add is that which is contained in the file identified by
# the "LICENSE" environment variable or defaults to the
# "source-code-license.txt" file in the current directory.
#
# Usage:
#
# add-license <java-file>
#
# Example:
#
#  To add a license to all currently unlicensed source code files:
#  % export LICENSE="$PWD/source-code-license.txt"
#  % export $TRUNK="<path to source code root>"
#  % find $TRUNK -name '*.java' -exec add-license \{\} \;
#
test -n "$LICENSE" || LICENSE="source-code-license.txt";

if ! test -e "$LICENSE"; then
  echo "Must identify the license file using env var 'LICENSE'"
  exit 1;
fi

sourcefile="$1";
curlicense=`head "$sourcefile" | grep 'Copyright'`;

if test -z "$curlicense"; then
  # add the license if it isn't present.
  echo "adding $LICENSE to $sourcefile";

  cat "$LICENSE" "$sourcefile" > "$sourcefile.licensed";
  mv -f "$sourcefile.licensed" "$sourcefile";
fi
